function [tZC, strResiduals, gZC, se_g] = Calculate(data, cutTimeAtStartIn_ms)
global ExperimentParameters;

sampleRate = ExperimentParameters.samplingFrequencies;
c = 2.99792458e8; % m/s
f = ExperimentParameters.laserFrequency; 
lambda = c/f;

% DC compensation
data2 = data(1e6:1.01e6);
data_dc = 0.5 * (max(data2) + min(data2));

data = data - data_dc;

% Zero-crossing calculation
tic;
temp = diff(sign(data));
indices = find(abs(temp)==2);
tSignChange = indices/sampleRate;
tShift = data(indices)./(data(indices+1)-data(indices))/sampleRate;
tZC = tSignChange - tShift;
indicesExactZeros = find(data==0);
tZC = [tZC indicesExactZeros/sampleRate];
tZC = sort(tZC);
fprintf('Zero-crossings calculated, cost %.1f seconds \n', toc);

%tZC = tZC(2:end-1);  %remove first and last zerocrossing for safety.
%distancesZC = (0:length(tZC)-1)*(lambda/4);
%% 2018.08.07 by YJM
% tZC = tZC(28e4:end);%xxms 190923 T2
% tZC = tZC(7e4:end);%xxms
% tZC = tZC(12e4:end);%xxms 190911
% tZC = tZC(5e4:end);%20ms 201008 T1
% tZC = tZC(2.5e4:end);%10ms 190930 T2
% tZC = tZC(5.5e4:end);%20ms 201008 T1
% tZC = tZC(2.0e4:end);%10ms 190930 T1
% tZC = tZC(1.5e4:end);%5ms 190926 T1
% tZC = tZC(5e3:end);%5ms 190926 T1
distancesZC = (0:length(tZC)-1)*(lambda/4);
%%

% _______________ cut data at the start of drop ___________
if cutTimeAtStartIn_ms > 0
   tZC(1 : length(find(tZC < cutTimeAtStartIn_ms/1000))) = []; 
   distancesZC(1 : length(find(tZC < cutTimeAtStartIn_ms/1000))) = [];
end

tic;
[pZC Str_X] = polyfit(tZC, distancesZC, 2);
fprintf('g value fitted, cost %.1f seconds \n', toc);

% calculate g, v, h
gZC = pZC(1,1)*2*1e8;
fprintf('Current g = %9.2f microgal \n', gZC);
% fprintf('******************************************** \n');
% fprintf(' \n');
vZC = pZC(1,2);
hZC = pZC(1,3);
distancesfit = hZC + vZC*tZC + 0.5*pZC(1,1)*2*tZC.^2;
strResiduals= distancesZC - distancesfit;

% yjm,20171130
invR=inv(Str_X.R);
d_f=Str_X.df;
n_r=Str_X.normr;

Cov_Matrix=invR*(invR)'*n_r^2/d_f;
se_g=2*sqrt(Cov_Matrix(1,1))*1e8; % uGal
se_v=sqrt(Cov_Matrix(2,2)); % m/s
se_s=sqrt(Cov_Matrix(3,3))*1e9; % nm

fprintf('SingleDropSE: %2.9f \n', se_g);
fprintf('******************************************** \n');
fprintf(' \n');
% yjm,20171130

end
